//
//  YLContinuationDetailController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//  续投详情

#import "YLContinuationDetailController.h"
#import "YLSubTitileNoArrowTableViewCell.h"
#import "YLContinuationDetailViewModel.h"
#import "YLGlobeHeader.h"
#import "GZActionSheet.h"
#import "YLRegularFinancingDetailController.h"
#import "YLRegularFinancingDetailModel.h"
@interface YLContinuationDetailController ()
@property (nonatomic, strong) YLContinuationDetailViewModel *viewModel;
@end

@implementation YLContinuationDetailController


- (YLContinuationDetailViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLContinuationDetailViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLSubTitileNoArrowTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"threePoint"] style:UIBarButtonItemStylePlain target:self action:@selector(more)];
    
    if (_contractCode) {
        __weak typeof(self) weakSelf = self;
        self.title = _contractCode;
        [[self.viewModel.requestCommand execute:_contractCode] subscribeNext:^(id  _Nullable x) {
            [weakSelf.tableView reloadData];
        }];
    }
    
    
   
}

- (void)more
{

    
    if (self.viewModel.detailModel) {
        NSArray *array = @[@"查看原订单",@"查看现订单"];
        __weak typeof(self) weakSelf = self;
        GZActionSheet *sheet = [[GZActionSheet alloc]initWithTitleArray:array andShowCancel:YES];
        //    /** 1. 代理方式 */
        //    sheet.delegate = self;
        
        /** 2. Block 方式 */
        // __weak typeof(self) weakSelf = self;
        sheet.ClickIndex = ^(NSInteger index){
            NSLog(@"Show Index %zi",index);
            if (index == 2) {
                // 查看现订单
                YLRegularFinancingDetailController *detail = [[YLRegularFinancingDetailController alloc] init];
                detail.contractCode = weakSelf.viewModel.detailModel.contractCode;
                [weakSelf.navigationController pushViewController:detail animated:YES];
            } else if (index == 1) {
                // 查看原订单
                YLRegularFinancingDetailController *detail = [[YLRegularFinancingDetailController alloc] init];
                detail.contractCode = weakSelf.viewModel.detailModel.previousContractCode;
                [weakSelf.navigationController pushViewController:detail animated:YES];
            }
            
        };
        
        [self.view.window addSubview:sheet];
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLSubTitileNoArrowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    cell.model = self.viewModel.models[indexPath.row];
    return cell;
}
@end
